/**
  ETFRoot project, Anyang Normal University && IMP-CAS
  \class ETFTaTrackManager
  \brief a manager class holding the fitted tracks and trackers. NOTE that this
  is a singleton class. The class is dedicated for P.Ma-style cosmic-ray tests
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2022-04-26
  \date 2022-04-26 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2022-04-26   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include "ETFTaTrackManager.h"
#include "ETFTaLTracker.h"
#include "ETFTaL2Tracker.h"
#include "ETFGlobalTracker.h"

ClassImp(ETFTaTrackManager);

ETFTaTrackManager *ETFTaTrackManager::fInstance = nullptr;

ETFTaTrackManager::ETFTaTrackManager() : ETFVTrackManager(){
  fPreTaTracker = new ETFTaLTracker("preTaLTracker", {"dcTaL0", "dcTaL1"}, "tstop4U");
  fPostTaTracker = new ETFTaLTracker("postTaLTracker", {"dcTaL2", "dcTaL3"}, "tstop4D");
  fTaTracker = new ETFTaL2Tracker("taLTracker", {"dcTaL0", "dcTaL1", "dcTaL2", "dcTaL3"},
    "tstop4U", "tstop4D");
} // end ctor

ETFTaTrackManager::~ETFTaTrackManager(){}

void ETFTaTrackManager::SetDebug(bool opt){
  fPreTaTracker->SetDebug(opt);
  fPostTaTracker->SetDebug(opt);
  fTaTracker->SetDebug(opt);
} // end member function SetDebug

ETFTaTrackManager *ETFTaTrackManager::Instance(){
  if(!fInstance) fInstance = new ETFTaTrackManager();
  return fInstance;
} // end member function Instance

// register tracks to main TTree
void ETFTaTrackManager::Register(){
  fPreTaTracker->Register();
  fPostTaTracker->Register();
  fTaTracker->Register();
} // end member function Register

// do the overall tracking
void ETFTaTrackManager::Map(){
  if(!fHasMapped) fHasMapped = true;
  else return;
  // do the preliminary tracking //
  fPreTaTracker->Map();
  fPostTaTracker->Map();
  fTaTracker->Map();
  // // then refine the tracking with ETFGlobalTracker //
  // fGlobalTracker->SetPostMagTrk(fPostMagTracker->GetTrack(0));
  // fGlobalTracker->Map();
  // // replace X tracks in pre/post-TaTrackers with ameliorated global ones
  // if(fGlobalTracker->GetNofTracks())
  //   fGlobalTracker->UpdateTracks(fPreTaTracker, fPostTaTracker);
} // end member function Map

// do the overall 3D tracking
void ETFTaTrackManager::Map3D(){
  if(!fHasMapped3D) fHasMapped3D = true;
  else return;

  Map(); // do the 2D X tracking first, and ameliorate the tracking
} // end member function Map

void ETFTaTrackManager::Draw(){
  fPreTaTracker->Draw();
  fPostTaTracker->Draw();
  fTaTracker->Draw();
} // end member function Draw

void ETFTaTrackManager::Write(){
  fPreTaTracker->Write();
  fPostTaTracker->Write();
  fTaTracker->Write();
  // fGlobalTracker->Write();
} // end member function Write

void ETFTaTrackManager::Reset(){
  fPreTaTracker->Reset();
  fPostTaTracker->Reset();
  fTaTracker->Reset();
  if(fHasMapped) fHasMapped = false;
  if(fHasMapped3D) fHasMapped3D = false;
} // end member function Write

void ETFTaTrackManager::Print(){
  fPreTaTracker->Print();
  fPostTaTracker->Print();
  fTaTracker->Print();
} // end member function Print
